home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12943 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: belle.bork.com!news
  2. From: root@belle.bork.com (BOFH)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character - strtok trick
  5. Followup-To: comp.lang.c,comp.unix.programmer
  6. Date: 3 Apr 1996 10:40:32 -0600
  7. Organization: Bork Systems Group
  8. Message-ID: <4ju9m0$nq9@belle.bork.com>
  9. References: <31616F63.481D@lava.weeg.uiowa.edu> <4jrvv3$ask@aimnet.aimnet.com> <828493319snz@genesis.demon.co.uk> <828542474.7672@tertio.demon.co.uk>
  10. NNTP-Posting-Host: belle.bork.com
  11. X-Newsreader: TIN [UNIX 1.3 BETA-950824-color PL0]
  12.  
  13. alord@tertio.co.uk wrote:
  14. : Although the behaviour of strtok() is well defined I try to avoid it like the
  15. : plague because it uses a static area to maintain state. If another function
  16. : happens to call it between successive invocations then your screwd.
  17.  
  18. Well, yes it does use a static data area to store it's state, but that
  19. doesn't matter.  When you call the function this way, strtok will return
  20. a pointer to the beginning of the string that it chopped a newline off
  21. of, AND will insert a NULL character into the location of the newline.
  22. After it returns, you are done.  Your string has had the newline taken
  23. off, and you are done with strtok.  strtok will internally remember where
  24. it last took a delimiter off, but it only needs it when strtok is called
  25. with a NULL buffer anyway.
  26.  
  27. The only problem with this is that you could be in the middle of parsing
  28. a string with strtok, and then you attempted to strtok(buffer,...), which
  29. would definately cause a problem.
  30.  
  31. My $0.02.
  32.  
  33.  
  34. Randy Scott <scottr@belle.bork.com>
  35.